const clickoutsideContext = '@@clickoutsideContext';

export default {
    beforeMount(el, binding, vnode) {
        const documentHandler = (e) => {
            console.log(e)
            if (vnode.el && !el.contains(e.target)) {
                el[clickoutsideContext].methodName();
            }
        };
        el[clickoutsideContext] = {
            documentHandler,
            methodName: binding.value,
            arg: binding.arg || 'click',
        };
        setTimeout(() => {

            document.addEventListener(el[clickoutsideContext].arg, documentHandler);
        }, 0)
    },

    update(el, binding) {
        console.log(el, binding)
        el[clickoutsideContext].methodName = binding.value;
    },

    unmounted(el) {
        document.removeEventListener(
            el[clickoutsideContext].arg,
            el[clickoutsideContext].documentHandler);
    },
};